// Copyright (C) 2011 - Tencent Inc.
// Author: Liu Shaohui (struggleliu@tencent.com)
//
// Created: 08/24/2011 09:46:51
// Description:

#include <stdio.h>
#include <string.h>

#include "thirdparty/gflags/gflags.h"
#include "thirdparty/glog/logging.h"
#include "thirdparty/google/profiler.h"
#include "thirdparty/google/heap-profiler.h"

DEFINE_int32(loop_num, 1000, "The loop num for the cpu example.");
DEFINE_string(prof_output_path, "./prof.prof", "The profile output path.");

int cpu_small() {
    char* buf = new char[100000];
    int sum = 0;
    for (int i = 0; i < 100000; i ++) {
        sum += i;
    }
    delete buf;
    return sum;
}

int cpu_large() {
    char* buf = new char[100000];
    int sum = 0;
    for (int i = 0; i < 400000; i ++) {
        sum += i;
    }
    delete buf;
    return sum;
}

int main(int argc, char** argv) {
    google::ParseCommandLineFlags(&argc, &argv, true);
    google::InitGoogleLogging(argv[0]);

    ProfilerStart(FLAGS_prof_output_path.c_str());
    HeapProfilerStart(FLAGS_prof_output_path.c_str());

    LOG(INFO) << "The loop num is: " <<  FLAGS_loop_num;
    for (int i = 0; i < FLAGS_loop_num; i++) {
        cpu_small();
        cpu_large();
    }
    HeapProfilerDump("");
    HeapProfilerStop();
    ProfilerStop();
    return 0;
}

